#ifndef _ODRIVE_BOARD_H__
#define _ODRIVE_BOARD_H__

/* 硬件能支持的最大母线电流 */
#define CONFIG_HW_MAX_DC_CURRENT     10.0f
/* 硬件能支持的最大/小母线电压 */
#define CONFIG_HW_MAX_DC_VOL         56.0f
#define CONFIG_HW_MIN_DC_VOL         20.0f
/* 能支持的最大转速，和计算能力有关 */
#define CONFIG_HW_MAX_MOTOR_RPM      20000.0f
/* 能支持的最大相电流 */
#define CONFIG_HW_MAX_PHASE_CURR     20.0f

#define CONFIG_VBUS_RATED_VOL        36.0f
/* 12bit adc */
#define CONFIG_ADC_FULL_MAX   4096
/* adc 参考模拟电压 */
#define CONFIG_ADC_REF_VOLTAGE   3.3F
/* 板子采集VW两相电流，下桥采样 */
#define CONFIG_PHASE_CURRENT_PHASE_BC 1
/* 使用USB OTG作为 CDC virtual serial */
#define CONFIG_USBOTG_DEVICE 1
/* 使用uart通信，包括usb serial */
#define CONFIG_BOARD_COM_UART 1

/* 使用ams 编码器 as5047p */
#define CONFIG_ENCODER_TYPE CONFIG_ENCODER_AMS

/* 选择使用hall还是编码器作为位置传感器 */
#define CONFIG_USE_HALL_ENCODER 0
#define CONFIG_USE_MAG_ENCODER 1

/* 是否有单独的母线电流传感器 */
#define CONFIG_DC_CURR_SAMPLE 0 //硬件实现母线采样
/* 没有mos温度采集 */
#define CONFIG_MOS_NTC_SENSOR 0
/* 有泄放功率电阻那边的温度采集 */
#define CONFIG_AUX_NTC_SENSOR 1

/* mcu相关设置 */
#define SYSTEM_CLOCK (168000000)
#define TIM_CLOCK (SYSTEM_CLOCK) 
#define NS_PER_TCLK (6u) /* (1/168000000 * 1000000000) */
#define NS_2_TCLK(ns) (((ns + NS_PER_TCLK - 1)/NS_PER_TCLK)) 
#define SystemCoreClockMHz (SYSTEM_CLOCK/1000000)

/*
	APB2 clk is 84M ->ADCCLK -> ADC_CLOCK_SYNC_PCLK_DIV4 ->21M
	采样时间3 cycle，12.5个转换时间, 15.5/21 <= 1us
	1us x TIM_CLK_MHz / 2 为 tim1 cc4的触发时间
*/
#define ADC_TIM_CC4_OFFSET ((TIM_CLOCK/1e6))
/* 设置svpwm的调制率，一般in phase电流采样可以设置为1，下桥采样，需要留出一定的时间给adc采样
   ADC 采样时间1uS，1us死区，加上避开mos管的开关点 400ns左右(对称pwm，左右都需要加，整个预留 2.8us，0.046)
*/
#define CONFIG_SVM_MODULATION   0.9f //(1.0f - 0.046f)

/* 配置 cpu ticks时钟，获取 1us的精度 */
#define CONFIG_CPU_TICK_TIMER TIM5
#define CPU_TICK_TIMER_CLKM (TIM_CLOCK/2/1e6)
#define CPU_TICK_TIMER_CLK_ENABLE()  __HAL_RCC_TIM5_CLK_ENABLE()

/* uvw三相驱动pwm频率 */
#define CONFIG_FOC_PWM_FREQ 24000
#define CONFIG_FOC_PWM_Period (TIM_CLOCK/CONFIG_FOC_PWM_FREQ)
#define CONFIG_FOC_PWM_Half_Period (CONFIG_FOC_PWM_Period>>1)
#define CONFIG_FOC_PWM_UPDATE_REPEAT 1 //在三角波的顶点更新3相的pwm占空比

/* pwm设置的死区，总的死区一般是 pwm设置的死区+硬件(gate driver)的死区 */
#define CONFIG_FOC_PWM_DEADTIME 150
#define CONFIG_FOC_HW_DeadTime (CONFIG_FOC_PWM_DEADTIME + 100/*8301*/)
#define CONFIG_PWM_DEADTIME_nCLKs NS_2_TCLK(CONFIG_FOC_PWM_DEADTIME)

/* 辅组泄放功率电阻 */
#define AUX_TIM_PERIOD_CLOCKS 4096  //used for aux res
#define AUX_TIM_DEADTIME_CLOCKS 40

#define PWM_TIMER TIM1
#define PWM_TIMER_CLK_ENABLE()  __HAL_RCC_TIM1_CLK_ENABLE()
#define PWM_Update_IrqHandler TIM1_UP_TIM10_IRQHandler
#define PWM_Brk_IrqHandler    TIM1_BRK_TIM9_IRQHandler
#define PWM_TIMER_UPDATE_nIRQ TIM1_UP_TIM10_IRQn
#define PWM_TIMER_BRK_nIRQ    TIM1_BRK_TIM9_IRQn
#define PWM_U_H_PIO PIO(PORTA, PIN8)
#define PWM_V_H_PIO PIO(PORTA, PIN9)
#define PWM_W_H_PIO PIO(PORTA, PIN10)
#define PWM_U_L_PIO PIO(PORTB, PIN13)
#define PWM_V_L_PIO PIO(PORTB, PIN14)
#define PWM_W_L_PIO PIO(PORTB, PIN15)
#define PWM_PIN_AF_FUNC  GPIO_AF1_TIM1

/* every one ms called */
#define MC_1MS_TIMER TIM7 //base timer
#define MC_1MS_TIMER_CLK_ENABLE()  __HAL_RCC_TIM7_CLK_ENABLE()
#define MC_1MS_TIMER_CLKM (TIM_CLOCK/2/1e6)
#define MC_1MS_IrqHandler TIM7_IRQHandler
#define MC_1MS_TIMER_nIRQ TIM7_IRQn

/* aux power discharger interface */
#define AUX_TIMER TIM2
#define AUX_TIMER_CLK_ENABLE()  __HAL_RCC_TIM2_CLK_ENABLE()
#define AUX_PWM_L_PIN PIO(PORTB, PIN10)
#define AUX_PWM_H_PIN PIO(PORTB, PIN11)

/* encoder or hall */
#if (CONFIG_USE_HALL_ENCODER)
#define HALL_IO1 PIO(PORTB, PIN4)
#define HALL_IO2 PIO(PORTB, PIN5)
#define HALL_IO3 PIO(PORTC, PIN9)
#elif (CONFIG_USE_MAG_ENCODER)
#define MAG_ENC_TIMER TIM3
#define MAG_ENC_TIMER_CLK_ENABLE()  __HAL_RCC_TIM3_CLK_ENABLE()
#define MAG_ENC_PIN_A PIO(PORTB, PIN4)
#define MAG_ENC_PIN_B PIO(PORTB, PIN5)
#define MAG_ENC_PIN_Z PIO(PORTC, PIN9)
#define MAG_TIMER_FILTER_VALUE 4
#define MAG_ENC_EXT_nCS_PIN PIO(PORTB, PIN2)      /* 外部编码器片选 */
#define MAG_ENC_INT_nCS_PIN PIO(PORTA, PIN15)     /* 内部as5047片选 */
#define MAG_ENC_Z_TIMER MAG_ENC_TIMER
#define MAG_ENC_Z_TIMER_CH TIM_CHANNEL_4
#define MAG_ENC_Z_IrqHandler TIM3_IRQHandler
#define MAG_ENC_Z_IRQ_FLAGS TIM_FLAG_CC4
#define MAG_ENC_Z_IRQ_INT TIM_IT_CC4
#define MAG_ENC_Z_nIRQ      TIM3_IRQn

#define MAG_ENC_nCS_PIN MAG_ENC_EXT_nCS_PIN

#define CONFIG_ENCODER_ABI 1
#define CONFIG_ENCODER_ABS 0
#define CONFIG_ENCODER_SPI 1

static __INLINE uint32_t timer_enc_z_cap_value(void) {
	return MAG_ENC_Z_TIMER->CCR4;
}
#endif

/* gate driver & current sensor */
#define DRV8301_nCS_PIN PIO(PORTC, PIN13)
#define DRV8301_ENA_PIN PIO(PORTB, PIN12)
#define DRV8301_FAULT_PIN PIO(PORTD, PIN2)
#define CONFIG_DRV8301_GAIN   40//0.9(margin) x 1.35(max out of amplifier) / 0.0005(shunt) / 60 (max phase current)

/* spi interface */
#define CONFIG_SPI_DEV SPI3
#define SPI_SCK_PIO PIO(PORTC, PIN10)
#define SPI_MISO_PIO PIO(PORTC, PIN11)
#define SPI_MOSI_PIO PIO(PORTC, PIN12)

//ADC sample u/v phase current, pc0(ADC123_IN10), pc1(ADC123_IN11)
#define IU_ADC_SAMPLE_CHAN  ADC_CHANNEL_10
#define IU_ADC_PIO     PIO(PORTC, PIN0)

#define IV_ADC_SAMPLE_CHAN  ADC_CHANNEL_11
#define IV_ADC_PIO     PIO(PORTC, PIN1)
/* adc数据转为电流值得系数 */
#define CONFIG_CURRENT_SENSOR_CEOF (CONFIG_ADC_REF_VOLTAGE/CONFIG_ADC_FULL_MAX/(float)CONFIG_DRV8301_GAIN/5e-4f)
/* 采样到的电流的极性 */
#define CONFIG_CURRENT_POLARITY 1

//VBUS PA6(ADC12_IN6)
#define VBUS_ADC_SAMPLE_CHAN  ADC_CHANNEL_6
#define VBUS_ADC_PIO    PIO(PORTA, PIN6)
#define CONFIG_VBUS_SENSOR_CEOF (CONFIG_ADC_REF_VOLTAGE/CONFIG_ADC_FULL_MAX*19.0F)

//motor temp sample PC5(ADC12_IN15)
#define MOTOR_TEMP_ADC_CHAN   ADC_CHANNEL_15
#define MOTOR_TEMP_ADC_PIO    PIO(PORTC, PIN5)
#define MOTOR_NTC_FROM_ADC(adc) (((1.0f - adc/(float)CONFIG_ADC_FULL_MAX)/(adc/(float)CONFIG_ADC_FULL_MAX)) * 3300.0f)

//aux temp sample PA5(ADC12_IN5)
#define AUX_TEMP_ADC_CHAN   ADC_CHANNEL_5
#define AUX_TEMP_ADC_PIO    PIO(PORTA, PIN5)
#define AUX_NTC_FROM_ADC(adc) (((1.0f - adc/(float)CONFIG_ADC_FULL_MAX)/(adc/(float)CONFIG_ADC_FULL_MAX)) * 3300.0f)

#define GPIO_READY_PIO 				INVALD_PIO
#define GPIO_CRUISE_PIO             INVALD_PIO
#define GPIO_SIDE_PIO               INVALD_PIO
#define GPIO_HIGH_GEAR              INVALD_PIO
#define GPIO_LOW_GEAR               INVALD_PIO
#define GPIO_R_GEAR                 INVALD_PIO

#define CONFIG_Motor_3505 1
#define CONFIG_ENCODER_CC_INVERT 1

#endif /* _ODRIVE_BOARD_H__ */
